package com.omybug.test;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

/**
 * @author: omybug
 * @date: 18/9/4 14:34
 */
public class GeneratorServiceEntity {

    @Test
    public void generateCode() {
        String packageName = "com.omybug.study";
        boolean serviceNameStartWithI = false;//user -> UserService, 设置成true: user -> IUserService
        generateByTables(serviceNameStartWithI, packageName, "student");
    }

    private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://192.168.1.199:3306/test";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("tuoyan520")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                .setNaming(NamingStrategy.underline_to_camel)   // 下划线转换
                .setInclude(tableNames);    //修改替换成你需要的表名，多个表名传数组

        // 设置Mapper父类
//        strategyConfig.setSuperMapperClass("com.omybug.core.mapper.IBaseMapper");
        // 设置Entity父类
        strategyConfig.setSuperEntityClass("com.omybug.study.entity.BaseModel");
        // 自定义实体，公共字段
        strategyConfig.setSuperEntityColumns(new String[] { "id", "created_at", "updated_at" , "is_deleted"});

        config.setActiveRecord(true)
                .setAuthor("oMyBug")
                .setOutputDir("/Users/omybug/MyCode/Java/study/springboot/src/main/java/")
                .setFileOverride(false);    // 禁止覆盖
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                ).execute();
    }

    private void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }

}
